###

The label displays text or it wraps an image.

###

class tree.ui.Label extends tree.ui.Widget
    constructor: (@text) ->
        super
        @htmlelem = document.createElement 'div'
        @htmlelem.setAttribute 'class', 'tree-label'
        @text ?= ''
        @has_image = false
        @image = null
        @htmlelem.innerHTML = @text
    
    set_text: (text) ->
        @text = text
        @has_image = false
        @image = null
        @htmlelem.innerHTML = @text
    
    get_text: () ->
        return @text
    
    set_image: (image) ->
        @image = image
        @has_image = true
        @htmlelem.innerHTML = @image.show()
    
    get_size: () ->
        x = 0
        y = 0
        css_width = window.getComputedStyle(this.htmlelem, null).getPropertyValue 'font-size'
        w = this.text.length * css_width.substring(0, css_width.length-2) / 2
        css_height = window.getComputedStyle(this.htmlelem, null).getPropertyValue 'line-height'
        h = css_width.substring 0, css_width.length - 2
        return new Rect x, y, w, h
    
    get_image: () ->
        if not @has_image
            return null
            
        else
            return @image
